package yaner.hotel.manager.resource.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yaner.hotel.manager.common.dto.role.RoleAddDto;
import yaner.hotel.manager.common.dto.role.RoleDto;
import yaner.hotel.manager.common.dto.role.RoleUpdateDto;
import yaner.hotel.manager.common.vo.RoleVo;
import yaner.hotel.manager.resource.dao.IRoleDao;
import yaner.hotel.manager.resource.entity.Role;
import yaner.hotel.manager.resource.mapper.IRoleMapper;
import yaner.hotel.manager.resource.service.IRoleService;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private IRoleDao roleDao;
    @Autowired
    private IRoleMapper roleMapper;

    @Override
    public List<RoleVo> roleList(RoleDto roleDto) {
        List<RoleVo> roleVos = new ArrayList<>();
        List<Role> roles = roleDao.selectList(
                new QueryWrapper<Role>().lambda()
                        .orderByDesc(Role::getUpdateTime)
        );
        roles.forEach(role -> {
            roleVos.add(roleMapper.toVo(role));
        });
        return roleVos;
    }

    @Override
    public Page<RoleVo> rolePage(RoleDto roleDto) {
        Page<RoleVo> roleVoPage = new Page<>();
        List<RoleVo> roleVos = new ArrayList<>();
        Page<Role> roles = roleDao.selectPage(
                new Page<>(roleDto.getCurrentPage(), roleDto.getPageSize()),
                new QueryWrapper<Role>().lambda()
                        .orderByDesc(Role::getUpdateTime)
        );
        roles.getRecords().forEach(role -> {
            RoleVo roleVo = roleMapper.toVo(role);
            roleVos.add(roleVo);
        });
        roleVoPage.setRecords(roleVos);
        roleVoPage.setTotal(roles.getTotal());
        roleVoPage.setPages(roles.getPages());
        roleVoPage.setCurrent(roles.getCurrent());
        roleVoPage.setSize(roles.getSize());
        return roleVoPage;
    }

    @Override
    public RoleVo getRoleInfo(String roleId) {
        Role role = roleDao.selectById(roleId);
        return roleMapper.toVo(role);
    }

    @Override
    public String addRoleInfo(RoleAddDto roleAddDto) {
        Role role = roleMapper.toAddEntity(roleAddDto);
        int i = roleDao.insert(role);
        return String.valueOf(i);
    }

    @Override
    public String updateRoleInfo(RoleUpdateDto roleUpdateDto) {
        Role role = roleMapper.toUpdateEntity(roleUpdateDto);
        role.setRoleId(roleUpdateDto.getRoleId());
        int i = roleDao.updateById(role);
        return String.valueOf(i);
    }

    @Override
    public String deleteRoleInfo(String roleId) {
        int i = roleDao.deleteById(roleId);
        return String.valueOf(i);
    }
}
